package com.edu.web.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.edu.web.comm.Constants;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

/**
 * @author piggsoft
 * @e-mail piggsoft@163.com
 * @since 2013-4-29
 * @version 1.0
 */
public class JsonUtils {
	public static String getJSONStringFromList(List<?> list){
		return JSONArray.fromObject(list, getCustomJsonConfig()).toString();
	}
	
	private static JsonConfig getCustomJsonConfig(){
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonValueProcessor() {
			@Override
			public Object processArrayValue(Object array, JsonConfig jsonConfig) {
				return null;
			}

			@Override
			public Object processObjectValue(String name, Object value,
					JsonConfig jsonConfig) {
				SimpleDateFormat format = new SimpleDateFormat(Constants.DATE_FORMAT_STRING);
				return format.format(value);
			}});
		return jsonConfig;
	}
	
	public static String getBaseString(String name, Object value) {
		return "{\"" + name + "\":" + "\"" + (value == null ? "" : value.toString()) + "\"}";
	}
}	
